package operation;

import Dao.UserDao;
import interfac.UserOperation;
import pojo.User;

import java.util.List;
import java.util.Scanner;

public class UpdateUserOperation implements UserOperation {
        @Override
    public void execute(Scanner scanner) {
        UserDao userDao = new UserDao();
        show_user(); // 显示所有员工，以便用户选择要修改的员工
        System.out.print("请输入要修改的员工ID: ");
        int userIdToUpdate = Integer.parseInt(scanner.nextLine());

        User existingUser = userDao.getUserById(userIdToUpdate);

        if (existingUser != null) {
            System.out.println("当前员工信息如下：");
            System.out.printf("ID: %d, 用户名: %s%n", existingUser.getId(), existingUser.getUsername());

            System.out.println("请选择要修改的信息：1. 用户名 2. 密码 3. 全部");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    System.out.print("请输入新的用户名: ");
                    String updatedUsername = scanner.nextLine();
                    existingUser.setUsername(updatedUsername);
                    break;
                case "2":
                    System.out.print("请输入新的密码: ");
                    String updatedPassword = scanner.nextLine();
                    existingUser.setPassword(updatedPassword);
                    break;
                case "3":
                    System.out.print("请输入新的用户名: ");
                    String fullUpdatedUsername = scanner.nextLine();
                    System.out.print("请输入新的密码: ");
                    String fullUpdatedPassword = scanner.nextLine();
                    existingUser.setUsername(fullUpdatedUsername);
                    existingUser.setPassword(fullUpdatedPassword);

                    break;
                default:
                    System.out.println("无效的选择，请重新输入。");
                    return;
            }

            System.out.print("确认要更新此员工的信息吗？(y/n): ");
            String confirmation = scanner.nextLine();
            if ("y".equalsIgnoreCase(confirmation)) {
                System.out.println("员工信息已成功更新");
            } else {
                System.out.println("取消更新操作");
            }
        } else {
            System.out.println("输入的员工ID不存在，请检查后重新输入。");
        }
    }

    private void show_user() {
        UserDao userDao = new UserDao();
        List<User> allUsers = userDao.getUsers();

        if (!allUsers.isEmpty()) {
            System.out.println("----- 员工列表 -----");
            for (User user : allUsers) {
                System.out.printf("ID: %d, 用户名: %s%n", user.getId(), user.getUsername());
            }
            System.out.println("-------------------");
        } else {
            System.out.println("没有找到任何员工记录。");
        }
    }
}